package com.zlq.cn.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zlq.cn.util.KeyUtil;
import com.zlq.cn.util.StringUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

@Getter
@Setter
public class Category {
    String sourceKey;
    Integer examMultipleScore;
    Integer passMark;
    String joinKey;
    String subDesc;
    String title;
    Integer containData;
    Integer fullMark;
    Integer parentId;
    Integer parentLevel;
    Integer examSingleScore;
    Integer state;
    Integer examTime;
    Integer examCount;
    String mainDesc;
    List<Category> child;
    Integer id;

    public static void main(String[] args) {
        String str = FileUtil.readString(new File("C:\\Users\\14988\\Desktop\\分类.txt"), "UTF-8");
        List<Category> categorys = JSON.parseArray(str, Category.class);
        List<Category> saveCategorys = new ArrayList<>();
        Queue<Category> myQueue = new LinkedList<>();
        myQueue.addAll(categorys);
        while (!myQueue.isEmpty()) {
            Category category = myQueue.poll();
            if (StringUtils.isNotBlank(category.joinKey)) {
                category.parentLevel = category.joinKey.split("_").length;
            }
            saveCategorys.add(category);
            if (CollectionUtils.isNotEmpty(category.child)) {
                myQueue.addAll(category.child);
            }
            category.setChild(null);
        }
        List<JSONObject> list = new ArrayList<>();
        System.out.println(saveCategorys);
        for (Category saveCategory : saveCategorys) {
            if (Objects.equals(saveCategory.getContainData(), 1)) {
                String key = "4657c4ab1f04c004abaf773f404ccc64";
                long l = System.currentTimeMillis();
                String joinKey = saveCategory.getJoinKey();
                Map<String, Object> param = new HashMap<>();
                param.put("appShort", "com.tqy.driving");
                param.put("appVersion", "2.1");
                param.put("channel", "yyb");
                param.put("joinKey", joinKey);
                param.put("timestamp", l);
                String collect = param.keySet().stream().sorted()
                        .map(it -> it + "=" + param.get(it))
                        .collect(Collectors.joining("&", key, key));
                param.put("sign", SecureUtil.md5(collect));
                System.out.println(joinKey);
                String post = HttpUtil.post("http://api.tbcr.cn/api/open/driving/category/data", param);
                String s = KeyUtil.AES_Decrypt(post, "6f66a72e1c8462efa4adbcbd528bfb34", KeyUtil.EncryptMode_Ecb);
                JSONObject jsonObject = JSON.parseObject(s);
                List<Integer> result = JSON.parseArray(jsonObject.getString("result"), Integer.class);
                if (CollectionUtils.isNotEmpty(result)) {
                    for (Integer id : result) {
                        JSONObject json = new JSONObject();
                        json.put("join_key", joinKey);
                        json.put("id", id);
                        list.add(json);
                    }
                }
            }
        }
        FileUtil.writeString(JSON.toJSONString(list), new File("C:\\Users\\14988\\Desktop\\分类绑定.json"), "UTF-8");
    }
}
